package com.jian.mygame;

import com.jian.mygame.GameView;
import com.jian.mygame.GameView.GameThread;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class MyGame extends Activity {
    /** A handle to the thread that's actually running the animation. */
    private GameThread mGameThread;

    /** A handle to the View in which the game is running. */
    private GameView mGameView;
        
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Window features must be set before setContentView() is called       
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        updateFullscreenStatus(true);

        // tell system to use the layout defined in our XML file
        setContentView(R.layout.game_landview);
        //setContentView(R.layout.main);

        
        // get handles to the LunarView from XML, and its LunarThread
        mGameView = (GameView) findViewById(R.id.view);
        mGameThread = mGameView.getThread();

        // give the LunarView a handle to the TextView used for messages
        //mGameView.setTextView((TextView) findViewById(R.id.text));

        //Set to change Media Volume during this activity instead of Ringer Volume
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        
        if (savedInstanceState == null) {
            // we were just launched: set up a new game
            //mGameThread.setState(GameThread.STATE_READY);
            Log.w(this.getClass().getName(), "SIS is null");
        } else {
            // we are being restored: resume a previous game
            //mGameThread.restoreState(savedInstanceState);
            Log.w(this.getClass().getName(), "SIS is nonnull");
        }
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
        Log.i("MyGame", "OnStart()");
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
        try {
			mGameView.StartGame();
			Log.i("MyGame", "OnResume()");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			Log.e("Exception", "Exception! " + e.getMessage());
			Log.e("Exception", "---Call Stack---\n");
			for( StackTraceElement ste : e.getStackTrace()) {
				Log.e("Exception", " " + ste.getClassName() + "." + ste.getMethodName()
						               + "(" + ste.getLineNumber() + ")\n");
			}
		}
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
    	mGameView.PauseGame();
    	Log.i("MyGame", "OnPause()");
    }
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
    	//mGameView.PauseGame();
    	Log.i("MyGame", "OnStop()");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    	//mGameView.StopMotionSensor();
    	Log.i("MyGame", "OnDestroy()");
    }
    
    
    private void updateFullscreenStatus(boolean bUseFullscreen)
    {   
       if(bUseFullscreen)
       {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        }
        else
        {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        //m_contentView.requestLayout();
    }
    
    /**
     * Notification that something is about to happen, to give the Activity a
     * chance to save state.
     * 
     * @param outState a Bundle into which this Activity should save its state
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // just have the View's thread save its state into our Bundle
        super.onSaveInstanceState(outState);
        //mLunarThread.saveState(outState);
        Log.i(this.getClass().getName(), "SIS called");
    }
    
    @Override  
    protected void onRestoreInstanceState(Bundle savedInstanceState) {  
      super.onRestoreInstanceState(savedInstanceState);  
       
    } 
    
}